﻿package com.pandemicode.framework.utils {
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	
	/**
	* @author Michael Lawrence
	* @version 1.0.0
	*/
	public class DisplayObjectHelper {
		/**
		 * A series of methods to help with various DisplayObject related tasks.
		 */
		public function DisplayObjectHelper() { }
		
		/**
		 * Sets the buttonMode and mouseChildren properties of all the Sprites passed to true.
		 * @param	...rest	The Sprites to make buttons.
		 */
		public static function makeButton(...rest):void {
			rest.filter(Filter.isSprite).forEach(_makeButton);
		}
		
		/**
		 * Sets the buttonMode and mouseChildren properties of all the MovieClips passed to true and makes the MovieClips change to the "Over" frame on rollover.
		 * @param	...rest	The MovieClips to make buttons.
		 */
		public static function makeRollOverButton(...rest):void {
			rest.filter(Filter.isMovieClip).forEach(_makeRollOverButton);
		}
		
		/**
		 * Sets the tabIndex property of all the InteractiveObjects passed to the order they are passed.
		 * @param	...rest	The InteractiveObjects to set the tabIndex of.
		 */
		public static function setTabIndex(...rest):void {
			rest.filter(Filter.isInteractiveObject).forEach(_setTabIndex);
		}
		
		/**
		 * Enables tab navigation from the specified InteractiveObjects.
		 * @param	...rest	The InteractiveObjects to set tabEnabled.
		 */
		public static function setTabEnabled(...rest):void {
			rest.filter(Filter.isInteractiveObject).forEach(_setTabEnabled);
		}
		
		/**
		 * Disables tab navigation from the specified InteractiveObjects.
		 * @param	...rest	The InteractiveObjects to set tabEnabled.
		 */
		public static function setTabDisabled(...rest):void {
			rest.filter(Filter.isInteractiveObject).forEach(_setTabDisabled);
		}
		
		/**
		 * Flips the specified objects horizontally.
		 * @param	...rest	The DisplayObjects to set flip horizontally.
		 */
		public static function flipHorizontal(...rest):void {
			rest.filter(Filter.isDisplayObject).forEach(_flipHorizontal);
		}
		
		/**
		 * Flips the specified objects vertically.
		 * @param	...rest	The DisplayObjects to set flip vertically.
		 */
		public static function flipVertical(...rest):void {
			rest.filter(Filter.isDisplayObject).forEach(_flipVertical);
		}
		
		private static function _flipHorizontal(element:DisplayObject, index:int, array:Array):void {
			var matrix:Matrix = element.transform.matrix;
			matrix.a = -1;
			matrix.tx = element.width + element.x;
			element.transform.matrix = matrix;
		}

		private static function _flipVertical(element:DisplayObject, index:int, array:Array):void {
			var matrix:Matrix = element.transform.matrix;
			matrix.d = -1;
			matrix.ty = element.height + element.y;
			element.transform.matrix = matrix;
		}
		
		private static function _makeButton(element:Sprite, index:int, array:Array):void {
			element.buttonMode = true;
			element.mouseChildren = false;
		}
		
		private static function _makeRollOverButton(element:MovieClip, index:int, array:Array):void {
			_makeButton(element, index, array);
			element.addEventListener(MouseEvent.ROLL_OVER, _rollOver);
			element.addEventListener(MouseEvent.ROLL_OUT, _rollOut);
		}
		
		private static function _rollOver(e:MouseEvent):void {
			try {
				(e.target.BG as MovieClip).gotoAndStop("Over");
			} catch (ex:Error) {
				trace(ex);
			}
		}
		
		private static function _rollOut(e:MouseEvent):void {
			try {
				(e.target.BG as MovieClip).gotoAndStop("Normal");
			} catch (ex:Error) {
				trace(ex);
			}
		}
		
		private static function _setTabIndex(element:InteractiveObject, index:int, array:Array):void {
			element.tabIndex = index;
		}
		
		private static function _setTabEnabled(element:InteractiveObject, index:int, array:Array):void {
			element.tabEnabled = true;
		}
		
		private static function _setTabDisabled(element:InteractiveObject, index:int, array:Array):void {
			element.tabEnabled = false;
		}
	}
}